﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Collections;
using System.Security.Cryptography;
using System.Text;

public partial class Main_ShopManage : System.Web.UI.Page
{
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    ShopDao sd = new ShopDao();
    PicDao pd = new PicDao();
    DataTable dt = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            ShopVo sv = new ShopVo();
            sv = sd.getUserShop(uv.Uid.ToString());
            UserBindData();
            ShopBindData(sv);
            PicBindData(sv.Sid.ToString());
        }
    }

    private void UserBindData()
    {
        labname.Text = uv.Uname;
        txttruename.Text = uv.Utruename;
        txtidcard.Text = uv.Uidcard;
        txtphone.Text = uv.Uphone;
        txtqq.Text = uv.Uqq;
        txtemail.Text = uv.Uemail;
    }

    private void ShopBindData(ShopVo sv)
    {
        dt = sd.getField();
        ddlfield.DataSource = dt;
        ddlfield.DataTextField = "Fname";
        ddlfield.DataValueField = "Fid";
        ddlfield.DataBind();
        labshopname.Text = sv.Sname;
        ddlfield.SelectedValue = sv.Fid.ToString();
        dt = sd.getType(sv.Fid.ToString());
        ddltype.DataSource = dt;
        ddltype.DataTextField = "Tname";
        ddltype.DataValueField = "Tid";
        ddltype.DataBind();
        ddltype.SelectedValue = sv.Tid.ToString();
        txtintrduce.Text = sv.Sintroduce.Replace("&nbsp;&nbsp;&nbsp;", " ").Replace("<br>", "\r\n"); ;
        txtcontent.Text = sv.Scontent.Replace("&nbsp;&nbsp;&nbsp;", " ").Replace("<br>", "\r\n"); ;
        txtjob.Text = sv.Sjob.Replace("&nbsp;&nbsp;&nbsp;", " ").Replace("<br>", "\r\n"); ;
        txtaddress.Text = sv.Saddress;
        txttel.Text = sv.Stel;
        if (sv.Slogo != "")
        {
            imglogo.ImageUrl = sv.Slogo;
            imglogo.Visible = true;
        }
        string[] array = sv.Spic.Split(new char[] { '|' });
        if (array[0].ToString() != "")
        {
            imgtop1.ImageUrl = array[0].ToString();
            imgtop1.Visible = true;
        }
        if (array[1].ToString() != "")
        {
            imgtop2.ImageUrl = array[1].ToString();
            imgtop2.Visible = true;
        }
        if (array[2].ToString() != "")
        {
            imgtop3.ImageUrl = array[2].ToString();
            imgtop3.Visible = true;
        }
        if (sv.Slicense != "")
        {
            imglicense.ImageUrl = sv.Slicense;
            imglicense.Visible = true;
        }
        if (sv.Sidcard != "")
        {
            imgidcard.ImageUrl = sv.Sidcard;
            imgidcard.Visible = true;
        }
    }

    private void PicBindData(string sid)
    {
        dt = pd.getPic(sid, "0");
        if (dt.Rows.Count > 0)
        {
            imginfo1.ImageUrl = dt.Rows[0][1].ToString();
            txtdesinfo1.Text = dt.Rows[0][4].ToString();
            imginfo1.Visible = true;
        }
        if (dt.Rows.Count > 1)
        {
            imginfo2.ImageUrl = dt.Rows[1][1].ToString();
            txtdesinfo2.Text = dt.Rows[1][4].ToString();
            imginfo2.Visible = true;
        }
        if (dt.Rows.Count > 2)
        {
            imginfo3.ImageUrl = dt.Rows[2][1].ToString();
            txtdesinfo3.Text = dt.Rows[2][4].ToString();
            imginfo3.Visible = true;
        }
        if (dt.Rows.Count > 3)
        {
            imginfo4.ImageUrl = dt.Rows[3][1].ToString();
            txtdesinfo4.Text = dt.Rows[3][4].ToString();
            imginfo4.Visible = true;
        }
        if (dt.Rows.Count > 4)
        {
            imginfo5.ImageUrl = dt.Rows[4][1].ToString();
            txtdesinfo5.Text = dt.Rows[4][4].ToString();
            imginfo5.Visible = true;
        }
        if (dt.Rows.Count > 5)
        {
            imginfo6.ImageUrl = dt.Rows[5][1].ToString();
            txtdesinfo6.Text = dt.Rows[5][4].ToString();
            imginfo6.Visible = true;
        }
        if (dt.Rows.Count > 6)
        {
            imginfo7.ImageUrl = dt.Rows[6][1].ToString();
            txtdesinfo7.Text = dt.Rows[6][4].ToString();
            imginfo7.Visible = true;
        }
        if (dt.Rows.Count > 7)
        {
            imginfo8.ImageUrl = dt.Rows[7][1].ToString();
            txtdesinfo8.Text = dt.Rows[7][4].ToString();
            imginfo8.Visible = true;
        }
    }

    protected void ddlfield_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddltype.Items.Clear();
        ddltype.Items.Add("--请选择--");
        dt = sd.getType(ddlfield.SelectedValue);
        ddltype.DataSource = dt;
        ddltype.DataTextField = "Tname";
        ddltype.DataValueField = "Tid";
        ddltype.DataBind();
    }
    protected void btnuplogo_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "logo", FileUplogo, Extensions, 50000, "Logo", imglogo);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender,e);
    }
    protected void btnuptop1_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop1, Extensions, 100000, "top1", imgtop1);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnuptop2_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop2, Extensions, 100000, "top2", imgtop2);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnuptop3_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".jpeg" };
        string msg = UploadImg("shop", "pic", FileUptop3, Extensions, 100000, "top3", imgtop3);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo1_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo1, Extensions, 200000, "info1", imginfo1);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo2_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo2, Extensions, 200000, "info2", imginfo2);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo3_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo3, Extensions, 200000, "info3", imginfo3);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo4_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo4, Extensions, 200000, "info4", imginfo4);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo5_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo5, Extensions, 200000, "info5", imginfo5);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo6_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo6, Extensions, 200000, "info6", imginfo6);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo7_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo7, Extensions, 200000, "info7", imginfo7);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }
    protected void btnupinfo8_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".gif", ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "img", FileUpinfo8, Extensions, 200000, "info8", imginfo8);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoppic_Click(sender, e);
    }

    protected void btnuplicense_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "license", FileUplicense, Extensions, 200000, "license", imglicense);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoplicense_Click(sender, e);
    }
    protected void btnupidcard_Click(object sender, EventArgs e)
    {
        string[] Extensions = { ".jpg", ".png", ".bmp", ".jpeg" };
        string msg = UploadImg("shop", "idcard", FileUpidcard, Extensions, 200000, "idcard", imgidcard);
        string script = "alert('" + msg + "');";
        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        btnshoplicense_Click(sender, e);
    }
    protected void btnupuserinfo_Click(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("../index.aspx");
        }
        uv = (UsersVo)Session["user"];
        bool flag = CheckSpecialWord(txttruename);
        if (flag)
        {
            string script = "alert('负责人姓名填写不正确！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        uv.Uemail = txtemail.Text;
        uv.Utruename = txttruename.Text;
        uv.Uidcard = txtidcard.Text;
        uv.Uphone = txtphone.Text;
        uv.Uqq = txtqq.Text;
        Exception ex = new Exception();
        ex = ud.updateShopUsers(uv);
        if (ex == null)
        {
            string script = "alert('修改用户资料成功！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            string script = "alert('修改用户资料失败！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }
    protected void btncanuserinfo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }
    protected void btnupshopinfo_Click(object sender, EventArgs e)
    {
        if (ddlfield.SelectedValue == "0")
        {
            string script = "alert('请选择所属领域！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        if (ddltype.SelectedValue == "0")
        {
            string script = "alert('请选择所属类型！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        uv = (UsersVo)Session["user"];
        ShopVo sv = new ShopVo();
        sv = sd.getUserShop(uv.Uid.ToString());
        sv.Fid = Convert.ToInt32(ddlfield.SelectedValue);
        sv.Tid = Convert.ToInt32(ddltype.SelectedValue);
        sv.Sintroduce = txtintrduce.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        sv.Scontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        sv.Sjob = txtjob.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
        sv.Saddress = txtaddress.Text;
        sv.Stel = txttel.Text;
        Exception ex = sd.updateSingleShop(sv);
        if (ex == null)
        {
            string script = "alert('修改店面信息成功！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            string script = "alert('修改店面信息失败！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }
    protected void btncanshopinfo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }
    protected void btnupshoppic_Click(object sender, EventArgs e)
    {
        ShopVo sv = new ShopVo();
        uv = (UsersVo)Session["user"];
        sv = sd.getUserShop(uv.Uid.ToString());
        sv.Slogo = imglogo.ImageUrl;
        sv.Spic = imgtop1.ImageUrl + "|" + imgtop2.ImageUrl + "|" + imgtop3.ImageUrl;
        Exception ex = sd.updateSingleShop(sv);
        if (ex == null)
        {
            pd.delShopPic(sv.Sid.ToString());
            //添加8张图片的信息
            PicVo pv = new PicVo();
            pv.Id = sv.Sid;
            pv.Ptype = 0;
            if (imginfo1.ImageUrl != "")
            {
                pv.Purl = imginfo1.ImageUrl;
                pv.Pname = txtdesinfo1.Text;
                pd.addPic(pv);
            }
            if (imginfo2.ImageUrl != "")
            {
                pv.Purl = imginfo2.ImageUrl;
                pv.Pname = txtdesinfo2.Text;
                pd.addPic(pv);
            }
            if (imginfo3.ImageUrl != "")
            {
                pv.Purl = imginfo3.ImageUrl;
                pv.Pname = txtdesinfo3.Text;
                pd.addPic(pv);
            }
            if (imginfo4.ImageUrl != "")
            {
                pv.Purl = imginfo4.ImageUrl;
                pv.Pname = txtdesinfo4.Text;
                pd.addPic(pv);
            }
            if (imginfo5.ImageUrl != "")
            {
                pv.Purl = imginfo5.ImageUrl;
                pv.Pname = txtdesinfo5.Text;
                pd.addPic(pv);
            }
            if (imginfo6.ImageUrl != "")
            {
                pv.Purl = imginfo6.ImageUrl;
                pv.Pname = txtdesinfo6.Text;
                pd.addPic(pv);
            }
            if (imginfo7.ImageUrl != "")
            {
                pv.Purl = imginfo7.ImageUrl;
                pv.Pname = txtdesinfo7.Text;
                pd.addPic(pv);
            }
            if (imginfo8.ImageUrl != "")
            {
                pv.Purl = imginfo8.ImageUrl;
                pv.Pname = txtdesinfo8.Text;
                pd.addPic(pv);
            }
            string script = "alert('修改店面图片成功！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            string script = "alert('修改店面图片失败！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }
    protected void btncanshoppic_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }
    protected void btnupshoplicense_Click(object sender, EventArgs e)
    {
        uv = (UsersVo)Session["user"];
        ShopVo sv = new ShopVo();
        sv = sd.getUserShop(uv.Uid.ToString());
        sv.Slicense = imglicense.ImageUrl;
        sv.Sidcard = imgidcard.ImageUrl;
        Exception ex = sd.updateSingleShop(sv);
        if (ex == null)
        {
            string script = "alert('修改实名图片成功！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            string script = "alert('修改实名图片失败！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }
    protected void btncanshoplicense_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }
    protected void btnuppwd_Click(object sender, EventArgs e)
    {
        if (txtoldpwd.Text == "" || txtpwd.Text == "" || txtpwdagain.Text == "")
        {
            string script = "alert('密码不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            uv = (UsersVo)Session["user"];
            txtoldpwd.Text = MD5(txtoldpwd.Text);
            if (txtoldpwd.Text != uv.Upwd)
            {
                string script = "alert('原密码错误！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            }
            else
            {
                Exception ex = new Exception();
                ex = ud.updateUsersPwd(uv.Uid.ToString(), txtpwd.Text);
                if (ex == null)
                {
                    string script = "alert('修改密码成功！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                }
                else
                {
                    string script = "alert('修改密码失败！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
                }
            }
        }
    }

    public static string MD5(string str)
    {
        byte[] b = Encoding.Default.GetBytes(str);
        b = new MD5CryptoServiceProvider().ComputeHash(b);
        string ret = "";
        for (int i = 0; i < b.Length; i++)
            ret += b[i].ToString("x").PadLeft(2, '0');
        return ret;
    }

    protected void btncanpwd_Click(object sender, EventArgs e)
    {
        Response.Redirect("../index.aspx");
    }

    public string UploadImg(string SorH, string type, FileUpload fu, string[] Extensions, int size, string info, Image img)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/" + SorH + "/" + type + "/") + fu.FileName;
        if (fu.FileName != "")
        {
            if (fu.HasFile)
            {
                string fileExtension = Path.GetExtension(fu.FileName).ToLower();
                string[] allowerExtensions = Extensions;
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                if (!fileOK)
                {
                    return "图片格式不正确！";
                }
                else
                {
                    if (fu.PostedFile.ContentLength <= size)
                    {
                        string picPath = path.Replace(fu.FileName, labshopname.Text + info + fileExtension);
                        fu.PostedFile.SaveAs(picPath);
                        bool flag = false;
                        //最后一部高级验证，图片上传后的操作，判断是否真的是图片
                        StreamReader sr = new StreamReader(picPath, Encoding.Default);
                        string strContent = sr.ReadToEnd();
                        sr.Close();
                        string str = "request|script|.getfolder|.createfolder|.deletefolder|.createdirectory|.deletedirectory|.saveas|wscript.shell|script.encode|server.|.createobject|execute|activexobject|language=";
                        foreach (string s in str.Split('|'))
                            if (strContent.IndexOf(s) != -1)
                            {
                                File.Delete(picPath);
                                flag = true;
                                break;
                            }
                        if (!flag)
                        {
                            img.ImageUrl = "../images/" + SorH + "/" + type + "/" + labshopname.Text + info + fileExtension;
                            img.Visible = true;
                            return "上传图片成功！";
                        }
                        else
                        {
                            return "图片格式非法！";
                        }
                    }
                    else
                    {
                        return "图片大小不能超过" + (size / 1000).ToString() + "K！";
                    }
                }
            }
            else
            {
                return "上传失败，格式不允许！";
            }
        }
        else
        {
            return "上传不能为空！";
        }
    }

    public bool CheckSpecialWord(TextBox txtbox)
    {
        bool flag = false;
        ArrayList list = new ArrayList();
        list.Add(" ");
        list.Add("~");
        list.Add("!");
        list.Add("@");
        list.Add("#");
        list.Add("$");
        list.Add("%");
        list.Add("^");
        list.Add("&");
        list.Add("*");
        list.Add("(");
        list.Add(")");
        list.Add("_");
        list.Add("+");
        list.Add("<");
        list.Add(">");
        list.Add(",");
        list.Add(".");
        list.Add("/");
        list.Add("\\");
        list.Add("?");
        list.Add(";");
        for (int i = 0; i < txtbox.Text.Length; i++)
        {
            if (list.Contains(txtbox.Text[i].ToString()))
            {
                flag = true;
            }
        }
        return flag;
    }
    protected void btnuserinfo_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
    }
    protected void btnshopinfo_Click(object sender, EventArgs e)
    {
        Panel2.Visible = true;
        Panel1.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
    }
    protected void btnshoppic_Click(object sender, EventArgs e)
    {
        Panel3.Visible = true;
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel4.Visible = false;
        Panel5.Visible = false;
    }
    protected void btnshoplicense_Click(object sender, EventArgs e)
    {
        Panel4.Visible = true;
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel5.Visible = false;
    }
    protected void btnupdatepwd_Click(object sender, EventArgs e)
    {
        Panel5.Visible = true;
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel4.Visible = false;
    }
}
